package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.entity.Major;
import com.edu.entity.Subject;
import com.edu.entity.SubjectScore;
import com.edu.mapper.SubjectMapper;
import com.edu.mapper.SubjectScoreMapper;
import com.edu.service.SubjectService;
import com.edu.vo.Result;
import com.edu.vo.SubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName SubjectImpl
 * @Description
 * @Author W.Jian
 * @Date 2021/5/22 13:04
 **/
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper,Subject> implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SubjectScoreMapper subjectScoreMapper;

    /**
     * 条件查询课程
     * @param subject
     * @return
     */
    @Override
    public List<Subject> listByCondition(Subject subject) {
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        if(subject.getSubjectType()!=null){
            queryWrapper.eq("subject_type",subject.getSubjectType());
        }
        return subjectMapper.selectList(queryWrapper);
    }

    @Override
    public Result insertRef(SubjectScore subjectScore) {
        if(subjectScore.getId()==null){
            HashMap<String, Object> param = new HashMap<>();
            param.put("del_flag",0);
            param.put("school_id",subjectScore.getSchoolId());
            param.put("major_id",subjectScore.getMajorId());
            param.put("name",subjectScore.getName());
            List<SubjectScore> subjectScoreList = subjectScoreMapper.selectByMap(param);
            if(!CollectionUtils.isEmpty(subjectScoreList)){
                return Result.failure("500","添加失败,该课程已经添加！");
            }
            subjectScoreMapper.insert(subjectScore);
        }else {
            HashMap<String, Object> param = new HashMap<>();
            param.put("del_flag",0);
            param.put("school_id",subjectScore.getSchoolId());
            param.put("major_id",subjectScore.getMajorId());
            param.put("name",subjectScore.getName());
            List<SubjectScore> subjectScoreList = subjectScoreMapper.selectByMap(param);
            if(!CollectionUtils.isEmpty(subjectScoreList)){
                return Result.failure("500","添加失败,该课程已经添加！");
            }else {
                subjectScoreMapper.updateById(subjectScore);
            }
        }
        return Result.success();
    }

    @Override
    public Result listSubjectRef(Long schoolId,Long majorId) {
        List<SubjectScore> subjects = subjectScoreMapper.selectByMajorId(schoolId,majorId);
        return Result.success(subjects,subjects.size());
    }

    @Override
    public Result updateDelFlag(Long subjectId) {
        SubjectScore subjectScore = new SubjectScore();
        subjectScore.setDelFlag(1);
        subjectScore.setId(subjectId);
        subjectScoreMapper.updateById(subjectScore);
        return Result.success();
    }

    @Override
    public List<SubjectVo> querySubjectScore(Long stuId, Long toMajorId) {
        return subjectScoreMapper.querySubjectScore(stuId,toMajorId);
    }
}
